<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Role;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpKernel\Exception\HttpException;

class UserController extends Controller
{
    // 初始化保存模型
    protected function initialize()
    {
        $this->model = new User();
    }

    public function index(Request $request)
    {
        $page = $request->query('page', 1);
        $pageSize = $request->query('pageSize', 10);
        $deptId = $request->query('deptId');
        $status = $request->query('status');
        $phone = $request->query('phone');
        $username = $request->query('username');

        $model = $this->model::with('dept:id,name');

        if ($deptId !== null) {
            $model = $model->where('deptId', $deptId);
        }

        if ($username !== null) {
            $model = $model->where('username',  'like',  "%{$username}%");
        }

        if ($phone !== null) {
            $model = $model->where('phone', 'like', "%{$phone}%");
        }

        if ($status !== null) {
            $model = $model->where('status', $status);
        }

        $model = $model->paginate(
            (int)$pageSize,
            ['*'], // 获取哪些字段，* 表示获取全部
            'page',
            (int)$page
        );

        $items = array_map(function ($user) {
            $user->dept = $user->dept ? [
                'id' => $user->dept->id,
                'name' => $user->dept->name
            ] : null;
            return $user;
        }, $model->items());

        return $this->successList(list: $items, total: $model->total());
    }

    public function store(Request $request)
    {
        $body = $request->json()->all();

        $body['password'] = Hash::driver('bcrypt')->make($body['password']);

        try {
            $model = $this->model::create($body);
            return $this->success(data: ['id' => $model->id]);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function update($id, Request $request)
    {
        $body = $request->json()->all();
        try {
            $this->model::where('id', $id)->update($body);
            return $this->success(message: '修改成功');
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function destroy($id)
    {
        try {
            $this->model::destroy($id);
            return $this->success(message: '删除成功');
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function batchDelete(Request $request)
    {
        $ids = $request->json('ids');
        try {
            $this->model::destroy($ids);
            return $this->success(message: '删除成功');
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function getAllrole()
    {
        $role = Role::select('id', 'name')->get();

        return $this->success(data: $role->toArray());
    }

    public function updatePassword(Request $request)
    {
        $id = $request->json('id');
        $password = $request->json('password');
        $password = Hash::driver('bcrypt')->make($password);

        try {
            $this->model::where('id', $id)->update(['password' => $password]);
            return $this->success(message: '密码重置成功');
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function getListRoleIds($id)
    {
        $model = $this->model::find($id);

        $roleIds = $model->roleIds;

        $data = $roleIds ? array_map(function ($value) {
            return (int)$value;
        }, explode(',', $roleIds)) : [];

        return $this->success(data: $data);
    }

    public function setRoleIds(Request $request)
    {
        $id = $request->json('id');

        $roleIds = $request->json('roleIds');

        $roleIds = implode(',', $roleIds);

        try {
            $this->model::where('id', $id)->update(['roleIds' => $roleIds]);
            return $this->success();
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
        var_dump($body);
    }
}
